/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package dBasea.backup;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class BabesKopia {
	private static int BUFFER = 10485760;

	// CrearBackup("127.0.0.1", "3306", "root", "pass", "opensecur", "
	public static boolean babesKopiaSortu(String host, String port,
			String user, String password, String db, String file_backup) {
		boolean ok = false;
		FileWriter fichero = null;

		String command = "mysqldump --host="
				+ host
				+ " --port="
				+ port
				+ " --user="
				+ user
				+ " --password="
				+ password
				+ " --default-character-set=utf8 --compact --complete-insert --extended-insert --skip-quote-names"
				+ " --skip-comments --skip-triggers " + db;

		try {
			// BackUp sortzeko sententzia
			Process run = Runtime.getRuntime().exec(command);

			// gordetzen da memorian backup

			InputStream in = run.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			StringBuffer temp = new StringBuffer();
			int count;
			char[] cbuf = new char[BUFFER];
			while ((count = br.read(cbuf, 0, BUFFER)) != -1)
				temp.append(cbuf, 0, count);
			br.close();
			in.close();

			fichero = new FileWriter(file_backup);
			PrintWriter pw = new PrintWriter(fichero);
			pw.println(temp.toString());
			ok = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (null != fichero)
					fichero.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return ok;
	}
}
